# 阶段1: 构建阶段
FROM debian:bookworm-slim AS builder

ENV TZ=Asia/Shanghai
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    tzdata \
    git \
    build-essential \
    autoconf \
    libreadline-dev \
    libssl-dev \
    wget \
    unzip \
    ca-certificates &&\
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    echo $TZ > /etc/timezone && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /build
ADD . .

RUN make

# 阶段2: 运行时镜像
FROM debian:bookworm-slim

ENV TZ=Asia/Shanghai
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    libssl3 \
    libgcc-s1 && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    echo $TZ > /etc/timezone && \
    rm -rf /var/lib/apt/lists/*

COPY --from=builder /build/silly /opt/
COPY --from=builder /build/lualib /opt/lualib
COPY --from=builder /build/luaclib /opt/luaclib
COPY --from=builder /build/deps/jemalloc/include/jemalloc/jemalloc.h /opt/include/jemalloc/
COPY --from=builder /build/src/*.h /opt/include/
COPY --from=builder /build/deps/lua/lua.hpp /opt/include/lua/
COPY --from=builder /build/deps/lua/luaconf.h /opt/include/lua/
COPY --from=builder /build/deps/lua/lua.h /opt/include/lua/
COPY --from=builder /build/deps/lua/lualib.h /opt/include/lua/
COPY --from=builder /build/deps/lua/lauxlib.h /opt/include/lua/

WORKDIR /app
ENTRYPOINT ["/opt/silly"]
